<?php
    $history  = Model_History::getList();
    $bookmark = Model_Bookmark::getList();
    if (($bookmark === false  || $bookmark->count() == 0) && ($history === false || $history->count() == 0)) {
        return;
    }
    $hide_saved_ads = false;
?>
<!--main_other is for the content that not related to main_content and main_more, such as the YourAsiaClassified-->
<?php
$grid_size = (boolean) (string) $this->placeholder('grid-size');
if (!$grid_size) {
    $grid_size = 16;
} else {
    $grid_size = (string) $this->placeholder('grid-size');
}?>
<div class="appendix grid_<?php echo $grid_size?>">
    <section class="yourAC">
        <h1><?php echo $this->t('Your AsiaClassified')?></h1>
        <p class="nav">
            
            <?php if ($history->count() != 0):?>
                <a href="#" id="history_list" class="selected"><?php echo $this->t('Recently Viewed Ads')?></a>
            <?php endif;?>
            <?php if ($bookmark->count() != 0):?> 
                <a href="#" id="bookmark_list" <?php echo $history->count() == 0 ? 'class="selected"' : ''?> ><?php echo $this->t('Saved Ads')?></a>
            <?php endif;?>
        </p>

        <?php //if($history->count()):?>
            <!--p class="clear-history"><a href="#" class="clear-history"><?php echo $this->t('Clear recently viewed ads')?></a></p-->
        <?php //endif;?>
        <p class="button em"><a href="<?php echo $this->geturl('post-an-ad', 'listing')?>"><?php echo $this->t('Post an Ad')?></a></p>

        <ul id="history" <?php echo $history->count() == 0 ? 'style="display:none"' : ''?> >
        <?php if ($history !== false):?>
                <li>
                <?php $count = 0;?>
                <?php foreach($history->limit(6) as $hist):?>
                    <?php $listing = $hist->listing?>
                    <?php if (empty($listing)) {continue;}?>
                    <?php if($listing->status != Model_Listing::STATUS_PUBLISHED) {continue;}?>
                    <?php $hide_saved_ads=true;?>
                    
                    <?php if ($count > 0 && $count % 3 == 0):?>
                        </li><li>
                    <?php endif?>
                    <?php $count++?>
                    <div class="yourAc-ad">
                        <h1>
                            <a href="<?php echo $listing->url()?>"><?php echo $listing->title?></a>
                        </h1>
                        <figure>
                            <a href="<?php echo $listing->url()?>">
                                <?php if ($listing->sold):?>
                                    <span class="sold">Sold!</span>
                                <?php endif;?>
                                
                                <img src="<?php echo $this->thumbnail()->scale($listing->featured_photo, 65, 65);?>">
                            </a>
                        </figure>
                        <?php if (!empty($listing->sale_price) || !empty($listing->rent_price)): ?>
                            <?php
                            if (!empty($listing->sale_price)) {
                                $sale_price = number_format($listing->sale_price);
                                ?><dd><?php echo $this->t('HKD __1__', null, $sale_price)?><em><?php echo $this->t('(Sale)')?></em></dd><?php
                            }
                            if (!empty($listing->rent_price)) {
                                $rent_price = number_format($listing->rent_price);
                                ?><dd><?php echo $this->t('HKD __1__', null, $rent_price)?><em><?php echo $this->t('(Rent)')?></em></dd><?php
                            }
                            ?>

                        <?php elseif (!empty($listing->is_free)):?>
                            <dd><?php echo $this->t('Free');?></dd>
                        <?php elseif (!empty($listing->is_contact)):?>
                            <dd><?php echo $this->t('Please contact');?></dd>
                        <?php elseif (!empty($listing->price)): ?>
                            <dd><?php echo $this->t('HKD __1__', null, number_format($listing->price));?></dd>
                        <?php endif;?>
                    </div>
                <?php endforeach;?>
                </li>
        <?php endif;?>
        </ul>
        <?php if ($bookmark !== false):?>
            <ul id="bookmark" <?php echo $hide_saved_ads ? 'class="hide"':''?>>
                <?php foreach($bookmark->limit(6) as $hist):?>
                    <?php $listing = $hist->listing?>
                    <?php if (empty($listing)) {continue;}?>
                    <li>
                        <div class="yourAc-ad">
                            <h1>
                                <a href="<?php echo $listing->url()?>"><?php echo $listing->title?></a>
                            </h1>
                            <figure>
                                <a href="<?php echo $listing->url()?>">
                                    <img src="<?php echo $this->thumbnail()->scale($listing->featured_photo, 65, 65);?>">
                                </a>
                            </figure>
                            <?php if (!empty($listing->sale_price) || !empty($listing->rent_price)): ?>
                                <?php
                                if (!empty($listing->sale_price)) {
                                    $sale_price = number_format($listing->sale_price);
                                    ?><dd><?php echo $this->t('HKD __1__',null, $sale_price)?><em><?php echo $this->t('(Sale)')?></em></dd><?php
                                }
                                if (!empty($listing->rent_price)) {
                                    $rent_price = number_format($listing->rent_price);
                                    ?><dd><?php echo $this->t('HKD __1__', null, $rent_price)?><em><?php echo $this->t('(Rent)')?></em></dd><?php
                                }
                                ?>

                            <?php elseif (!empty($listing->is_free)):?>
                                <dd><?php echo $this->t('Free');?></dd>
                            <?php elseif (!empty($listing->is_contact)):?>
                                <dd><?php echo $this->t('Please contact');?></dd>
                            <?php elseif (!empty($listing->price)): ?>
                                <dd><?php echo $this->t('HKD __1__', null, number_format($listing->price));?></dd>
                            <?php endif;?>
                        </div>
                    </li>
                <?php endforeach;?>
            </ul>
        <?php endif;?>
        <div class="option">	
	            <?php if($history->count()):?>
	                <a href="#" class="clear-history"><?php echo $this->t('Clear recently viewed ads')?></a>
	            <?php endif;?>
	            <?php if($bookmark->count()):?>
	                <a href="#" class="clear-bookmark hide"><?php echo $this->t('Clear Saved Ads')?></a>
	            <?php endif;?>
            <?php if ($history !== false):?>
                <?php
                    $count = $history->count();
                ?>
                <div class="page" id="page_history_bookmark" rel="1">
                    <?php for ($i = 0; $i < ceil($count/6); $i++):?>
                        <a class="yourac_page yourac_page_history" href="#" rel="<?php echo $i?>"><?php echo $i+1?></a>
                    <?php endfor;?>
                </div>
            <?php endif;?>
            <?php if ($bookmark !== false && !$hide_saved_ads):?>
                <?php
                    $count = $bookmark->count();
                ?>
                <div class="page" id="page_history_bookmark" rel="2">
                    <?php for ($i = 0; $i < ceil($count/6); $i++):?>
                        <a class="yourac_page yourac_page_bookmark" href="#" rel="<?php echo $i?>"><?php echo $i+1?></a>
                    <?php endfor;?>
                </div>
            <?php endif;?>
            <!--div class="page_info">0 - 10 / 853 Listings</div-->

        </div>
    </section>
</div>
